package com.boyuan.huijiaoyu;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.boyuan.huijiaoyu.R;
import com.magicsoft.app.entity.RememberEntity;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.app.helper.StringUtils;
import com.magicsoft.app.helper.ToastHelper;
import com.magicsoft.app.wcf.AccountService;
import com.magicsoft.app.wcf.listener.PostRecordResponseListener;
import com.ttcaca.cas.android.client.Main;
import com.ttcaca.cas.android.client.model.User;

/**
 * 修改密码
 * 
 * @author cheng
 * 
 */
public class AccountChangePswActivity extends BaseActivity implements
		OnClickListener {

	private Button btnBack;
	private TextView txt_title;
	private Button btn_change_psw;// 保存
	private EditText edit_oldpsw,// 旧密码
			edit_newpsw;// 新密码
	private AccountService accountService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_change_psw_activity);
		prepareView();
	}

	private void prepareView() {
		btnBack = (Button) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		txt_title = (TextView) findViewById(R.id.txt_title);
		txt_title.setText("修改密码");

		btn_change_psw = (Button) findViewById(R.id.btn_change_psw);
		btn_change_psw.setOnClickListener(this);

		edit_oldpsw = (EditText) findViewById(R.id.edit_oldpsw);
		edit_newpsw = (EditText) findViewById(R.id.edit_newpsw);

	}

	// 修改密码
	private void setChangePsw() {
		String oldpswString = edit_oldpsw.getText().toString();
		final String newpsw = edit_newpsw.getText().toString();

		final RememberEntity entity = SharePreferenceHelper
				.GetRememberEntity(getApplicationContext());
		String oldPassword = entity.getPsw();

		if (StringUtils.isEmpty(oldpswString) || oldpswString.length() < 6
				|| oldpswString.length() > 16) {
			ToastHelper.showMsg(getApplicationContext(), "请输入6-16位的旧密码", false);
			return;
		}
		if (!oldPassword.equals(oldpswString)) {
			ToastHelper.showMsg(getApplicationContext(), "请输入正确旧密码", false);
			return;
		}
		if (StringUtils.isEmpty(newpsw) || newpsw.length() < 6
				|| newpsw.length() > 16) {
			ToastHelper.showMsg(getApplicationContext(), "请输入6-16位的新密码", false);
			return;
		}

		if (oldpswString.equals(newpsw)) {
			ToastHelper.showMsg(getApplicationContext(), "新密码不能与旧密码相同", false);
			return;
		}

		if (accountService == null) {
			accountService = new AccountService(getApplicationContext());
		}
		showLoading(getString(R.string.loading_data));
		accountService.changePassword(entity.getAdmin(), newpsw,
				new PostRecordResponseListener() {

					@Override
					public void onFinish(String succString) {
						// hideLoading();
						ToastHelper.showMsg(AccountChangePswActivity.this,
								succString, false);

						if (entity != null) {
							entity.setPsw(newpsw);
							SharePreferenceHelper.saveRememberEntiy(
									getApplicationContext(), entity);

							new LoginTask().execute(entity.getAdmin(), newpsw);
						}

					}

					@Override
					public void onFailed(String failString) {
						hideLoading();
						ToastHelper.showMsg(AccountChangePswActivity.this,
								failString, false);

					}
				});
	}

	class LoginTask extends AsyncTask<String, Integer, String> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			// showLoading("登录中...");
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub

			String username = params[0];
			String password = params[1];

			try {
				User userinfo = Main.c.login(username, password);
				Main.c.check(Main.c.getTgc());
				System.out.println("用户信息：" + userinfo);
				System.out.println("tgc：" + Main.c.getTgc());

				RememberEntity entity = new RememberEntity();
				entity.setAdmin(username);
				entity.setPsw(password);
				SharePreferenceHelper.saveRememberEntiy(
						getApplicationContext(), entity);

				SharePreferenceHelper.saveAccount(getApplicationContext(),
						userinfo);
				SharePreferenceHelper.setStringValue(
						AccountChangePswActivity.this, "COOKIE",
						Main.c.getTgc());
				return "OK";
			} catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println(e.getMessage());

				return e.getMessage();
			}

		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			hideLoading();
			if ("OK".equals(result)) {

				finish();
			} else {
				ToastHelper.showMsg(AccountChangePswActivity.this, result,
						false);
			}
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		// 保存
		case R.id.btn_change_psw:
//			setChangePsw();
			break;
		default:
			break;
		}
	}

	@Override
	public
	void backClicked() {

		if (!isShowingLoading()) {
			isBackAllowed = true;
		}
	}

}
